package com.example.Arithmetic.Arithmetic;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 日期：2023/12/31
 * 时间：14:19
 * 描述：活动选择问题，最短时间内选择更多活动
 */
public class ActivitySelectionProblem {
    static class Activity {
        int index;
        int start;
        int finish;

        public Activity(int index, int start, int finish) {
            this.index = index;
            this.start = start;
            this.finish = finish;
        }

        public int getFinish() {
            return finish;
        }

        @Override
        public String toString() {
            return "Activity(" + index + ")";
        }
    }
    public static void main(String[] args) {
//        Activity[] activities = new Activity[]{
//                new Activity(0, 1, 3),
//                new Activity(1, 2, 4),
//                new Activity(2, 3, 5)
//        };
        Activity[] activities = new Activity[]{
                new Activity(0, 1, 2),
                new Activity(1, 3, 4),
                new Activity(2, 0, 6),
                new Activity(3, 5, 7),
                new Activity(4, 8, 9),
                new Activity(5, 5, 9)
        };
        Arrays.sort(activities, Comparator.comparingInt(Activity::getFinish));
//        System.out.println(Arrays.toString(activities));
        select(activities, activities.length);

    }

    private static void select(Activity[] activities, int length) {
        Activity prev = activities[0];
        List<Activity> list=new ArrayList<>();
        list.add(prev);
        for (int i = 1; i <length; i++) {
            Activity activity = activities[i];
            if (activity.start>=prev.finish){
                list.add(activity);
            }
        }
        System.out.println(list);
    }
}
